package com.wxy.school.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxy.school.common.R;
import com.wxy.school.dto.NoticeDto;
import com.wxy.school.entity.Notice;
import com.wxy.school.service.NoticeService;
import com.wxy.school.utils.BaseContext;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 王祥宇
 * @projectName school
 * @packageName com.wxy.school.controller
 * @data 2023/3/6 14:46
 * @description: 餐厅公告Controller层
 * @version: 1.0
 */

@RestController
@RequestMapping("school/notice")
@Slf4j
public class NoticeController {

    @Resource
    private NoticeService noticeService;

    /**
     * @description: 根据分页信息获取公告板块的所有公告信息
     * @author: 王祥宇
     * @param: pageNum 查询页数
     * @param: pageSize 每页查询数据条数
     * @param: qurey 模糊查询参数
     * @return: com.wxy.school.common.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page>
     */
    @GetMapping("/getNoticeList")
    public R<Page> get(int pageNum, int pageSize, String qurey){
        //构建分页查询构造器
        Page<Notice> noticePage = new Page<>(pageNum, pageSize);
        //构建查询条件
        LambdaQueryWrapper<Notice> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //当qurey变量不为空时添加该查询条件
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(qurey), Notice::getTitle, qurey);
        //构建分页查询条件
        lambdaQueryWrapper.orderByDesc(Notice::getUpdateTime);
        //执行查询
        noticeService.page(noticePage, lambdaQueryWrapper);
        //返回查询结果
        return R.success(noticePage);
    }

    /**
     * @description: 发布公告信息
     * @author: 王祥宇
     * @param: noticeDto
     * @return: com.wxy.school.common.R<java.lang.String>
     */
    @PutMapping("/addNotice")
    public R<String> add(@RequestBody NoticeDto noticeDto){
        Notice notice = new Notice();
        //拷贝数据
        BeanUtils.copyProperties(noticeDto, notice, "userName");
        //将登录用户加入到线程中
        BaseContext.setThreadLocal(noticeDto.getUserName());
        //构建查询器，查询该公告，判断该公告是否已经存在
        LambdaQueryWrapper<Notice> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Notice::getTitle, notice.getTitle());
        Notice notice1 = noticeService.getOne(lambdaQueryWrapper);
        if (notice1 == null){
            noticeService.save(notice);
            return R.success("公告发布成功");
        }else {
            return R.error("公告已存在");
        }
    }

    /**
     * @description: 修改公告信息
     * @author: 王祥宇
     * @param: noticeDto
     * @return: com.wxy.school.common.R<java.lang.String>
     */
    @PutMapping("/updateNotice")
    public R<String> update(@RequestBody NoticeDto noticeDto){
        Notice notice = new Notice();
        //拷贝数据
        BeanUtils.copyProperties(noticeDto, notice, "userName");
        //将登录用户加入到线程中
        BaseContext.setThreadLocal(noticeDto.getUserName());
        noticeService.updateById(notice);
        return R.success("公告修改成功");
    }

    /**
     * @description: 删除公告信息
     * @author: 王祥宇
     * @param: id
     * @return: com.wxy.school.common.R<java.lang.String>
     */
    @DeleteMapping("/deleteNotice/{id}")
    public R<String> delete(@PathVariable Long id){
        noticeService.removeById(id);
        return R.success("公告删除成功");
    }

    /**
     * @description: 获取所有公告信息
     * @author: 王祥宇
     * @return: com.wxy.school.common.R<java.util.List<com.wxy.school.entity.Notice>>
     */
    @GetMapping("/getAllNoticeList")
    public R<List<Notice>> getAll(){
        LambdaQueryWrapper<Notice> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Notice::getCreateTime);
        List<Notice> noticeList = noticeService.list(lambdaQueryWrapper);
        return R.success(noticeList);
    }

    /**
     * @description: 根据id获取公告信息
     * @author: 王祥宇
     * @param: noticeId
     * @return: com.wxy.school.common.R<com.wxy.school.entity.Notice>
     */
    @GetMapping("/getById/{noticeId}")
    public R<Notice> getById(@PathVariable Long noticeId){
        Notice notice = noticeService.getById(noticeId);
        return R.success(notice);
    }
}
